import sqlite3
from flask import jsonify, make_response
class mod: 
    def __init__(self):
        self.name = "SQLite"
        self.args = []
    def init(self, args):
        self.db = sqlite3.connect("db.sqlite")
        self.db.execute("CREATE TABLE IF NOT EXISTS storage (id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT UNIQUE NOT NULL, value TEXT)")
        self.db.close()
    def handle(self, userargs, request):
        self.db = sqlite3.connect("db.sqlite")
        if "setkey" in userargs and "setvalue" in userargs:
            self.cursor = self.db.cursor()
            self.cursor.execute("INSERT OR REPLACE INTO storage (key, value) VALUES (?, ?)", (userargs["setkey"], userargs["setvalue"]))
            self.db.commit()
            return '{"status": "200", "message": "OK"}'
        elif "getvalue" in userargs:
            self.cursor = self.db.cursor()
            self.cursor.execute("SELECT value FROM storage WHERE key = ?", (userargs["getvalue"],))
            return '{"status": "200", "message": "OK", "value": "%s"}' % self.cursor.fetchone()[0]
        elif "delkey" in userargs:
            self.cursor = self.db.cursor()
            self.cursor.execute("DELETE FROM storage WHERE key = ?", (userargs["delkey"],))
            self.db.commit()
            return '{"status": "200", "message": "OK"}'

        else:
            return "args missing"